package com.betty.cscams.service.impl;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;
import com.betty.cscams.dao.PsyQuestionMapper;
import com.betty.cscams.entity.PsyQuestion;
import com.betty.cscams.service.PsyQuestionService;
import com.betty.cscams.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * ---------------------------
 *  心理测评题目Service实现类(PsyQuestionServiceImpl)         
 * ---------------------------
 */
@Service
public class PsyQuestionServiceImpl implements PsyQuestionService {

    @Autowired
    private PsyQuestionMapper psyQuestionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(PsyQuestion record) {
        record.setLastUpdateBy(SecurityUtils.getUsername());
        record.setLastUpdateTime(new Date());
        if(record.getId() == null || record.getId() == 0) {
            // 新增记录
            record.setCreateBy(SecurityUtils.getUsername());
            record.setCreateTime(new Date());
            record.setDelFlag(0);
            
            // 如果排序号为空，则查询当前学期最大排序号+1
            if(record.getSortNum() == null) {
                Integer maxSortNum = psyQuestionMapper.findMaxSortNum(record.getTermId());
                record.setSortNum(maxSortNum == null ? 1 : maxSortNum + 1);
            }
            
            return psyQuestionMapper.add(record);
        }
        return psyQuestionMapper.update(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(PsyQuestion record) {
        return psyQuestionMapper.delete(record.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(List<PsyQuestion> records) {
        for(PsyQuestion record : records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public PsyQuestion findById(Long id) {
        return psyQuestionMapper.findById(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        Object termId = pageRequest.getParam("termId");
        Object question = pageRequest.getParam("question");
        
        // 处理参数，确保不为null
        String termIdStr = termId != null ? termId.toString() : null;
        String questionStr = question != null ? question.toString() : null;
        
        return MybatisPageHelper.findPage(pageRequest, psyQuestionMapper, "findPage", 
                termIdStr, questionStr);
    }

    @Override
    public List<PsyQuestion> findByTermId(Long termId) {
        return psyQuestionMapper.findByTermId(termId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int importQuestions(List<PsyQuestion> questions, Long termId) {
        int count = 0;
        if(questions != null && !questions.isEmpty()) {
            for(PsyQuestion question : questions) {
                question.setTermId(termId);
                question.setCreateBy(SecurityUtils.getUsername());
                question.setCreateTime(new Date());
                question.setLastUpdateBy(SecurityUtils.getUsername());
                question.setLastUpdateTime(new Date());
                question.setDelFlag(0);
                
                // 如果排序号为空，则查询当前学期最大排序号+1
                if(question.getSortNum() == null) {
                    Integer maxSortNum = psyQuestionMapper.findMaxSortNum(termId);
                    question.setSortNum(maxSortNum == null ? 1 : maxSortNum + 1);
                }
                
                psyQuestionMapper.add(question);
                count++;
            }
        }
        return count;
    }
} 